package com.ds.dsm.view.config.action;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.bean.CustomViewBean;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@Controller
@RequestMapping(path = "/dsm/view/config/field/action/")
public class FieldAction {

    @MethodChinaName(cname = "向上")
    @RequestMapping(method = RequestMethod.POST, value = "moveUP")
    @CustomAnnotation(imageClass = "spafont spa-icon-move-up", index = 0)
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, customRequestData = RequestPathEnum.treegridrow)
    public @ResponseBody
    ResultModel<Boolean> moveUP(String sourceClassName, String methodName, String fieldname, String domainId, String viewInstId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);

            MethodConfig customMethodAPIBean = classConfig.getSourceConfig().getMethodByName(methodName);
            CustomViewBean customViewBean = (CustomViewBean) customMethodAPIBean.getView();
            List<String> fieldNames = new ArrayList<>();
            List<String> displayFieldNames = customViewBean.getDisplayFieldNames();
            int k = displayFieldNames.indexOf(fieldname);
            if (k < displayFieldNames.size()) {
                displayFieldNames.add(k - 1, displayFieldNames.remove(k));
            }

            fieldNames.addAll(displayFieldNames);
            fieldNames.addAll(customViewBean.getHiddenFieldNames());

            LinkedHashSet<String> setNames = new LinkedHashSet<>();

            for (String displayName : fieldNames) {
                setNames.add(displayName);
            }

            customViewBean.setFieldNames(setNames);
            DSMFactory.getInstance().getAggregationManager().updateApiClassConfig(classConfig.getSourceConfig());
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    @MethodChinaName(cname = "向下")
    @RequestMapping(method = RequestMethod.POST, value = "moveDown")
    @CustomAnnotation(imageClass = "spafont spa-icon-move-down", index = 1)
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, customRequestData = RequestPathEnum.treegridrow)
    public @ResponseBody
    ResultModel<Boolean> moveDown(String sourceClassName, String methodName, String fieldname, String domainId, String viewInstId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);

            MethodConfig customMethodAPIBean = classConfig.getSourceConfig().getMethodByName(methodName);
            CustomViewBean customViewBean = (CustomViewBean) customMethodAPIBean.getView();

            List<String> fieldNames = new ArrayList<>();
            List<String> displayFieldNames = customViewBean.getDisplayFieldNames();
            int k = displayFieldNames.indexOf(fieldname);
            if (k < displayFieldNames.size()) {
                displayFieldNames.add(k+1, displayFieldNames.remove(k));
            }

            fieldNames.addAll(displayFieldNames);
            fieldNames.addAll(customViewBean.getHiddenFieldNames());

            LinkedHashSet<String> setNames = new LinkedHashSet<>();

            for (String displayName : fieldNames) {
                setNames.add(displayName);
            }

            customViewBean.setFieldNames(setNames);
            DSMFactory.getInstance().getAggregationManager().updateApiClassConfig(classConfig.getSourceConfig());
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


//
//    @MethodChinaName(cname = "更新字段")
//    @RequestMapping(method = RequestMethod.POST, value = "updateField")
//    @CustomAnnotation(imageClass = "spafont spa-icon-save", index = 2)
//    @APIEventAnnotation(callback = {CustomCallBack.Reload}, bindMenu = CustomMenuItem.saveRow, customRequestData = RequestPathEnum.treegridrow)
//    public @ResponseBody
//    ResultModel<Boolean> updateField(String sourceClassName, String methodName, String caption, String fieldname, ComponentType componentType, ComboInputType inputType, String viewInstId) {
//        ResultModel<Boolean> result = new ResultModel<Boolean>();
//        try {
//            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
//            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
//            MethodConfig customMethodAPIBean = customESDClassAPIBean.getMethodByName(methodName);
//
//            CustomViewBean customViewBean = (CustomViewBean) customMethodAPIBean.getView();
//            Map<String, ESDFieldConfig> fieldConfigMap = customViewBean.getFieldConfigMap();
//            FieldFormConfig fieldFormConfig = (FieldFormConfig) fieldConfigMap.get(fieldname);
//            fieldFormConfig.getAggConfig().setCaption(caption);
//
//            if (componentType.equals(ComponentType.ComboInput)) {
//                WidgetBean widgetBean = fieldFormConfig.getWidgetConfig();
//                if (widgetBean instanceof ComboBoxBean) {
//                    ComboBoxBean comboBoxBean = (ComboBoxBean) fieldFormConfig.getWidgetConfig();
//                    if (comboBoxBean.getInputType() != null && inputType != null && !comboBoxBean.getInputType().equals(inputType)) {
//                        SimpleComboBean fieldBean = new SimpleComboBean();
//                        fieldBean.setInputType(inputType);
//                        fieldFormConfig.setWidgetConfig(fieldBean);
//                    }
//                }
//            } else if (!fieldFormConfig.getComponentType().equals(componentType)) {
//                fieldFormConfig.setWidgetConfig(null);
//            }
//            fieldFormConfig.getAggConfig().setComponentType(componentType);
//            DSMFactory.getInstance().getAggregationManager().updateApiClassConfig(customESDClassAPIBean);
//            //  DSMFactory.getInstance().getViewManager().updateViewEntityConfig(classConfig);
//        } catch (JDSException e) {
//            e.printStackTrace();
//        }
//        return result;
//    }

}

